﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WXApi.Model;
using WxTenpay.Common;
using WxTenpay.Service.Model;
using WxTenpay.Service;

namespace WXApi.Controllers.SysApi
{

    /// <summary>
    /// 个人会员中心
    /// </summary>
    /// 
    [Route("[controller]/[action]")]
    [ApiController]
    [Action]
    public class UsersApiController : ReqDataControllerBase
    {


        private readonly ISysUser sys_user;
        private readonly ISysLog syslog;
        private readonly ISysLoginLog sysloginlog;
        public UsersApiController(ISysLog _syslog, ISysLoginLog _sysloginlog, ISysUser _sysuser)
        {
            sys_user = _sysuser;

            syslog = _syslog;
            sysloginlog = _sysloginlog;
        }


        /// <summary>
        /// 获取基本信息 
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpPost]
        public ActionResult GetUsermodel()
        {
            try
            {

                var token = HttpContext.Items["token"].ToString();
                if (string.IsNullOrWhiteSpace(token))
                    return Error("参数错误！");

                var _token = GetUser(token);//获取用户信息
                var model = sys_user.GetModel(x => x.GuId == _token);

                if (model == null)
                    return Error("参数错误!");


                var data = new
                {
                    model.UserName,
                    model.Name,
                    model.NickName,
                    model.GuId,

                };


                return Success(data);
            }
            catch (Exception ex)
            {
                ErrorLogDto model = new ErrorLogDto();
                model.data = "系统错误！";
                model._syslog = syslog;
                model.logmodel = new SysLogEntity { Action = "UsersApi/GetUsermodel", Content = ex.ToString(), Name = "会员中心获取基本信息" };
                return ErrorLog(model);

            }
        }

        /// <summary>
        /// 修改基本信息 
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpPost]
        public ActionResult UpdateUsermodel([FromBody] SysUserEntity entity)
        {
            try
            {

                var token = HttpContext.Items["token"].ToString();
                if (string.IsNullOrWhiteSpace(token))
                    return Error("参数错误！");

                var _token = GetUser(token);//获取用户信息
                var model = sys_user.GetModel(x => x.GuId == _token);

                if (model == null)
                    return Error("参数错误!");


                if (model.GuId != entity.GuId)
                    return Error("没有相关权限!");

                model.Name = entity.Name;
                model.NickName = entity.NickName;


                sys_user.Update(model);


                return Success("操作成功！");
            }
            catch (Exception ex)
            {
                ErrorLogDto model = new ErrorLogDto();
                model.data = "系统错误！";
                model._syslog = syslog;
                model.logmodel = new SysLogEntity { Action = "UsersApi/GetUsermodel", Content = ex.ToString(), Name = "会员中心获取基本信息" };
                return ErrorLog(model);

            }
        }


        /// <summary>
        /// 修改密码 
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpPost]
        public ActionResult UpdateUserpwd([FromBody] UserDto user)
        {
            try
            {

                var token = HttpContext.Items["token"].ToString();
                if (string.IsNullOrWhiteSpace(token))
                    return Error("参数错误！");

                var _token = GetUser(token);//获取用户信息
                var model = sys_user.GetModel(x => x.GuId == _token);

                if (string.IsNullOrWhiteSpace(user.Vcode))
                    return Error("验证码异常！");

                var code = DESEncrypt.MD5Decrypt(user.Vcode);
                if (user.Code.ToUpper() != code.ToUpper())
                    return Error("验证码错误！");


                string pwd = DESEncrypt.MD5Encrypt(user.Pwd);

                if (model.Password != pwd)
                    return Error("旧密码错误！");


                model.Password = DESEncrypt.MD5Encrypt(user.NPwd);

                sys_user.Update(model);


                return Success("操作成功！");
            }
            catch (Exception ex)
            {
                ErrorLogDto model = new ErrorLogDto();
                model.data = "系统错误！";
                model._syslog = syslog;
                model.logmodel = new SysLogEntity { Action = "UsersApi/GetUsermodel", Content = ex.ToString(), Name = "会员中心获取基本信息" };
                return ErrorLog(model);

            }
        }


    }
}
